//
//  HomeViewController.m
//  zhijian
//
//  Created by lcb on 2019/1/18.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "HomeViewController.h"
#import "KeyTableViewCell.h"
#import "AreaTableViewCell.h"
#import "CategoryTableViewCell.h"
#import "SourceResultViewController.h"
#import "SourceDetailViewController.h"
#import "SourceViewCell.h"
#import <PYSearch/PYSearch.h>
@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource,PYSearchViewControllerDelegate,PYSearchViewControllerDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)NSInteger currentKey;
@property(nonatomic,assign)NSInteger currentType;
@property(nonatomic,strong)NSString *kindsList;
@property(nonatomic,strong)PYSearchViewController *searchViewController;
@property(nonatomic,strong)NSMutableArray *searchDataArray;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentType=1;
    self.currentType =1;
    
    UIView*titleView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH-20,35)];
    titleView.userInteractionEnabled=YES;
    UISearchBar*searchBar = [[UISearchBar alloc]init];
    searchBar.frame=CGRectMake(0,0,SCREEN_WIDTH-20,35);
    searchBar.layer.cornerRadius=18;
    searchBar.layer.masksToBounds=YES;
    //边框线粗细
    [searchBar.layer setBorderWidth:8];
    //设置边框为白色是为了盖住UISearchBar上的灰色
    [searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];
    searchBar.placeholder=@"请输入搜索的关键字";
    searchBar.userInteractionEnabled=YES;
    [titleView addSubview:searchBar];
    UIButton *tag =[UIButton buttonWithType:UIButtonTypeCustom];
    tag.frame=titleView.frame;
    [tag addTarget:self action:@selector(searchbar) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:tag];
    self.navigationItem.titleView =titleView;
    [self initui];

}

-(void)initui
{
    //
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
     _tableView.separatorColor = UIColorOfHex(0xECECEC);
    [self.view addSubview:_tableView];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 120;
    }else if (indexPath.row==1) {
        return 90;
    }else if (indexPath.row==2) {
        return 160;
    }
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    if (indexPath.row==0) {
        KeyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeyTableViewCell"];
        if (!cell) {
            cell = [[KeyTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"KeyTableViewCell"];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            __weak __typeof(self)weakSelf = self;
            [cell setKeySelectedBlock:^(NSInteger index) {
                weakSelf.currentKey =index+1;
            }];
        }
        return cell;
    }else  if (indexPath.row==1) {
        AreaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AreaTableViewCell"];
        if (!cell) {
            cell = [[AreaTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"AreaTableViewCell"];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            __weak __typeof(self)weakSelf = self;
            [cell setAreaSelectedBlock:^(NSInteger index) {
                weakSelf.currentType =index+1;
                NSIndexPath *indexPath=[NSIndexPath indexPathForRow:2 inSection:0];
                [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            }];
        }
        return cell;
    } else  if (indexPath.row==2) {
        CategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CategoryTableViewCell"];
        if (!cell) {
            cell = [[CategoryTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"CategoryTableViewCell"];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            __weak __typeof(self)weakSelf = self;
            [cell setCateSelectedBlock:^(NSString * _Nonnull tags) {
                weakSelf.kindsList = tags;
            }];
        }
        cell.type =self.currentType;
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TEXTCELL"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"TEXTCELL"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    return cell;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 80;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    view.backgroundColor = [UIColor clearColor];
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.frame =CGRectMake(20, 40, SCREEN_WIDTH-40, 40);
    sendButton.layer.cornerRadius =20;
    sendButton.layer.masksToBounds = YES;
    sendButton.backgroundColor=navColor;
    [sendButton setTitle:@"查询" forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:sendButton];
    return view;
}
-(void)searchClick{
    SourceResultViewController *result =[[SourceResultViewController alloc] init];
    result.keyType=[NSString stringWithFormat:@"%ld",self.currentKey];
    result.sourceType=[NSString stringWithFormat:@"%ld",self.currentType];
    result.kindsList=self.kindsList;
    [self.navigationController pushViewController:result animated:YES];
}

//
-(void)searchbar
{
    _searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:@[] searchBarPlaceholder:@"关键字搜索"];
    // 4. 设置代理
    _searchViewController.delegate = self;
    _searchViewController.dataSource=self;
    _searchViewController.showHotSearch = NO;
    UIImage *image = [UIImage imageNamed:@"back"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    _searchViewController.backBarButtonItem =barButton;
    _searchViewController.searchViewControllerShowMode = PYSearchResultShowModePush;
    [self.navigationController pushViewController:self.searchViewController animated:YES];
}
-(void)backAction{
    self.searchViewController.searchBar.delegate=nil;
    self.searchViewController.searchBar.text =@"";
    [self.searchViewController.navigationController popViewControllerAnimated:YES];
}
#pragma mark - PYSearchViewControllerDelegate
- (void)searchViewController:(PYSearchViewController *)searchViewController
      didSearchWithSearchBar:(UISearchBar *)searchBar
                  searchText:(NSString *)searchText
{
    NSLog(@"-----didSearchWithSearchBar-----------%@",searchText);
    if (searchText.length) { // 与搜索条件再搜索
        [self search:searchText];
    }
}
- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
{
    NSLog(@"-----searchTextDidChange-----------%@",searchText);
    if (searchText.length) { // 与搜索条件再搜索
        [self search:searchText];
    }
}
- (UITableViewCell *)searchSuggestionView:(UITableView *)searchSuggestionView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseViewCell *  cell = [[SourceViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SourceViewCell"];
    SourceModel *qModel =[self.searchDataArray objectAtIndex:indexPath.row];
    cell.baseModel=qModel;
    return cell;
}
- (NSInteger)searchSuggestionView:(UITableView *)searchSuggestionView numberOfRowsInSection:(NSInteger)section
{
    return self.searchDataArray.count;
}

- (NSInteger)numberOfSectionsInSearchSuggestionView:(UITableView *)searchSuggestionView
{
    return 1;
}
- (CGFloat)searchSuggestionView:(UITableView *)searchSuggestionView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}
- (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchSuggestionAtIndexPath:(NSIndexPath *)indexPath
                   searchBar:(UISearchBar *)searchBar
{
    SourceModel *qModel =[self.searchDataArray objectAtIndex:indexPath.section];
    SourceDetailViewController *list =[[SourceDetailViewController alloc] init];
    list.sourceModel =qModel;
    [self.navigationController pushViewController:list animated:YES];
}
-(void)didClickBack:(PYSearchViewController *)searchViewController
{
    self.searchViewController.searchBar.delegate=nil;
    self.searchViewController.searchBar.text =@"";
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)didClickCancel:(PYSearchViewController *)searchViewController
{
    self.searchViewController.searchBar.delegate=nil;
    self.searchViewController.searchBar.text =@"";
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)search:(NSString*)text
{
    __weak __typeof(self)weakSelf = self;
    [[Api sharedApi] getSouceList:@"1" pageSize:@"20" keyType:[NSString stringWithFormat:@"%ld",self.currentKey] key:text sourceType:[NSString stringWithFormat:@"%ld",self.currentType] kindsList:self.kindsList  callback:^(ApiResponse *response,NSString* errorMsg) {
        if (response!=nil && response.code==0 && response.data) {
            NSArray * listModel = [NSArray yy_modelArrayWithClass:[SourceModel class] json:response.data];
            [weakSelf.searchDataArray removeAllObjects];
            weakSelf.searchDataArray = [NSMutableArray arrayWithArray:listModel];
        }else{
            [BaseToast show:response.message];
        }
        [weakSelf.searchViewController.searchSuggestionView reloadData];
    }];
}
@end
